package com.zan.juc;

import java.util.concurrent.CountDownLatch;

/**
 * @Author Zan
 * @Create 2024/3/1 10:02
 * @Description :
 */
public class CountDownLatchDemo {
    // 6个同学陆续离开教室之后，班长才可以锁门
    public static void main(String[] args) throws InterruptedException {

        // 创建CountDownLatch对象，设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);

        // 6个同学陆续离开教室
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 号同学离开了教室");

                // 让计数器 - 1，countDown()表示每次计数器的值-1
                countDownLatch.countDown();

            }, String.valueOf(i)).start();
        }
        // 等待 (没有为0就一直等待，为0才唤醒，继续往下走)
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + " 班长锁门走人");
    }
}
